package com.colorful.insuranceproject.consts;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.SparseArray;

import com.colorful.insuranceproject.R;
import com.lovely3x.common.requests.BaseCodeTable;
import com.lovely3x.common.utils.Response;

/**
 * 码表
 * Created by lovely3x on 16/7/26.
 */
public class CodeTable extends BaseCodeTable {

    private final Context mContext;

    public CodeTable(Context context) {
        this.mContext = context.getApplicationContext() == null ? context : context.getApplicationContext();
        initDescPool();
    }

    /**
     * 不支持的用户
     */
    public static final int UNSUPPORTED_USER = -1001;

    /**
     * 用户已经取消操作
     */
    private static final int ERROR_USER_CANCELED_CODE = -10002;

    /**
     * 网络异常
     */
    private static final int ERROR_NETWORK_EXCEPTION = -10004;

    /**
     * 无法理解服务端返回的数据
     */
    private static final int ERROR_CAN_NOT_PARSE_DATA = -10005;

    /**
     * 无网络
     */
    private static final int ERROR_NO_NET_WORK = -1006;

    /**
     * 服务器响应错误
     */
    private static final int ERROR_SERVER_RESPONDED_ERR = -1007;

    /**
     * 未知错误
     */
    private static final int ERROR_UNKNOWN_ERROR = -1008;

    /**
     * 错误码：服务器内部异常
     */
    private static final int ERROR_RESP_CODE_INTERNAL_ERR = -1009;

    /**
     * 错误码：请求的地址没有找到
     */
    private static final int ERROR_RESP_CODE_NOT_FOUND = -1010;

    /***
     * 注册成功但是登录失败
     */
    public static final int ERROR_REGISTER_SUCCESS_LOGIN_FAIL = -1011;

    /**
     * 需要绑定绑定电话号码
     */
    public static final int ERROR_NEED_BIND_PHONE_NUMBER = 20041;

    /**
     * 账号或密码错误
     */
    public static final int ERROR_ACCOUNT_OR_PASSWORD_INCORRECT = 10003;

    /**
     *  请求成功
     */
    public static final int CODE_SUCCESS = 1;

    /**
     *  错误码：账号不存在!
     */
    public static final int ACCOUNT_NOT_REGISTER = -1;

    /**
     *  错误码：账号已被停用!
     */
    public static final int ACCOUNT_IS_DISABLED = -2;

    /**
     * 账号或密码错误
     */
    public static final int ACCOUNT_OR_PASSWORD_ERROR = -3;

    /**
     * 没有登录
     */
    public static final int ERROR_NOT_LOGIN = -999;

    /**
     * 空数据
     */
    public static final int ERROR_EMPTY_DATA = 20008;

    /**
     * 该动态还未点赞，不能取消
     */
    public static final int ERROR_CAN_NOT_CANCEL_PRAISE_NOT_PRAISE = 20048;

    private final SparseArray<String> descTable = new SparseArray<>();

    private void initDescPool() {
        descTable.put(ACCOUNT_NOT_REGISTER, mContext.getString(R.string.account_not_regsiter));
        descTable.put(ACCOUNT_IS_DISABLED, mContext.getString(R.string.account_is_disabled));
        descTable.put(ERROR_CAN_NOT_PARSE_DATA, mContext.getString(R.string.noting_to_show));
        descTable.put(ERROR_UNKNOWN_ERROR, mContext.getString(R.string.unknown_error));
        descTable.put(ACCOUNT_OR_PASSWORD_ERROR, mContext.getString(R.string.acct_or_pwd_is_incorrect));
        descTable.put(ERROR_NETWORK_EXCEPTION, mContext.getString(R.string.network_exception));
        descTable.put(ERROR_NO_NET_WORK, mContext.getString(R.string.network_unavailable));
        descTable.put(ERROR_RESP_CODE_NOT_FOUND, mContext.getString(R.string.page_not_found));
        descTable.put(ERROR_RESP_CODE_INTERNAL_ERR, mContext.getString(R.string.server_internal_err));
        descTable.put(ERROR_ACCOUNT_OR_PASSWORD_INCORRECT, mContext.getString(R.string.acct_or_pwd_is_incorrect));
        descTable.put(ERROR_EMPTY_DATA, mContext.getString(R.string.noting_to_show));
        descTable.put(ERROR_CAN_NOT_CANCEL_PRAISE_NOT_PRAISE, mContext.getString(R.string.cant_praise_because_not_praise_at_before));
    }

    @Override
    public int getNoNetWorkErrorCode() {
        return ERROR_NO_NET_WORK;
    }

    @Override
    public int getUnknownErrorCode() {
        return ERROR_UNKNOWN_ERROR;
    }

    @Override
    public int getResponseErrorCode(int responseCode) {
        switch (responseCode) {
            case 404:
                return ERROR_RESP_CODE_NOT_FOUND;
            case 500:
                return ERROR_RESP_CODE_INTERNAL_ERR;
        }
        return ERROR_SERVER_RESPONDED_ERR;
    }


    @NonNull
    @Override
    public String getCodeDescription(int code) {
        return descTable.get(code);
    }

    @Override
    public String getCodeDescription(Response response) {
        String message = decode(response.errorMsg);
        return !TextUtils.isEmpty(message) ? message :
                getCodeDescription(response.errorCode);
//        return !TextUtils.isEmpty(response.errorMsg) ? response.errorMsg :
//                getCodeDescription(response.errorCode);
    }

    /**
     *    转换UNICODE
     */
    public static String decode(String unicodeStr) {
        if (unicodeStr == null) {
            return null;
        }
        StringBuffer retBuf = new StringBuffer();
        int maxLoop = unicodeStr.length();
        for (int i = 0; i < maxLoop; i++) {
            if (unicodeStr.charAt(i) == '\\') {
                if ((i < maxLoop - 5) && ((unicodeStr.charAt(i + 1) == 'u') || (unicodeStr.charAt(i + 1) == 'U')))
                    try {
                        retBuf.append((char) Integer.parseInt(unicodeStr.substring(i + 2, i + 6), 16));
                        i += 5;
                    } catch (NumberFormatException localNumberFormatException) {
                        retBuf.append(unicodeStr.charAt(i));
                    }
                else
                    retBuf.append(unicodeStr.charAt(i));
            } else {
                retBuf.append(unicodeStr.charAt(i));
            }
        }
        return retBuf.toString();
    }

    @Override
    public int getParseExceptionErrorCode(Exception e) {
        return ERROR_CAN_NOT_PARSE_DATA;
    }

    @Override
    public int getNetWorkExceptionCode() {
        return ERROR_NETWORK_EXCEPTION;
    }

    @Override
    public int getSuccessfulCode() {
        return CODE_SUCCESS;
    }

    @Override
    public int getJPushTagExceptionCode() {
        return 0;
    }

    @Override
    public int getClearUserDataFailureCode() {
        return 0;
    }

    @Override
    public int getNotLoginCode() {
        return ERROR_NOT_LOGIN;
    }

    @Override
    public int getEmptyDataCode() {
        return ERROR_EMPTY_DATA;
    }

    @Override
    public int getCanceledExceptionCode() {
        return ERROR_USER_CANCELED_CODE;
    }

    @Override
    public int getNotBindPhoneCode() {
        return ERROR_NEED_BIND_PHONE_NUMBER;
    }
}
